package com.kitchen.service.impl;

import com.kitchen.common.vo.ResultVO;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.beans.entity.Menu;
import com.kitchen.mapper.MenuMapper;
import com.kitchen.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 78468
 * 菜谱业务逻辑实现
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO showRecipe(String fName) {
        ResultVO resultVO = new ResultVO();
        try {
            List<Menu> menus = menuMapper.selectByFNameOrFIngredients(fName);
            if (!menus.isEmpty()) {
                resultVO.setCode(ResStatus.SUCCESS);
                resultVO.setMsg(ResStatus.SUCCESSFULLY_SEARCHED_FOR_RECIPES);
                resultVO.setData(menus);
                return resultVO;
            }
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.NO_RECIPES_OR_INGREDIENTS);
        } catch (Exception e) {
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.recipe_search_failed);
        }
        return resultVO;
    }
}
